package com.zl.freight.utils;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.widget.Toast;

import com.baidu.mapapi.model.LatLng;
import com.baidu.mapapi.navi.BaiduMapAppNotSupportNaviException;
import com.baidu.mapapi.navi.BaiduMapNavigation;
import com.baidu.mapapi.navi.NaviParaOption;
import com.zl.freight.mode.NavigationMode;
import com.zl.zlibrary.utils.PackageUtils;

import java.net.URISyntaxException;
import java.net.URLEncoder;

/**
 * Created by zhanglei on 2017/12/14.
 * 导航工具类
 */

public class NavigationUtils {

    public static void start(Context context, NavigationMode mode) {

        //判断百度地图是否被安装
        boolean isAppInstall = PackageUtils.getIsAppInstall(context, "com.baidu.BaiduMap");
        boolean isGaoDeInstall = PackageUtils.getIsAppInstall(context, "com.autonavi.minimap");
        if (!isAppInstall && !isGaoDeInstall) {
            Toast.makeText(context, "请先安装百度地图或高德地图", Toast.LENGTH_SHORT).show();
            return;
        }

        if (isAppInstall) {//首选百度
            baiduNavigation(context, mode);
        } else {//没按百度用高德
            startNaviGao(context, mode);
        }
    }

    /**
     * 百度导航
     *
     * @param context
     * @param mode
     */
    private static void baiduNavigation(Context context, NavigationMode mode) {
        try {
            LatLng start = new LatLng(mode.getStartLatitude(), mode.getStartLongitude());
            LatLng end = new LatLng(mode.getEndLatitude(), mode.getEndLongitude());
            // 构建 导航参数
            NaviParaOption para = new NaviParaOption()
                    .startPoint(start)
                    .endPoint(end)
                    .startName(mode.getStartAddress())
                    .endName(mode.getEndAddress());


            // 调起百度地图骑行导航
            BaiduMapNavigation.openBaiduMapBikeNavi(para, context);
        } catch (Exception e) {
            Toast.makeText(context, "地图导航失败，请检查参数是否正确", Toast.LENGTH_SHORT).show();
            e.printStackTrace();
        }
    }

    /**
     * 高德导航
     *
     * @param context
     * @param mode
     */
    private static void startNaviGao(Context context, NavigationMode mode) {
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("androidamap://navi?");
        try {
            //填写应用名称
            stringBuilder.append("sourceApplication=" + URLEncoder.encode("始发地", "utf-8"));
            //导航目的地
            stringBuilder.append("&poiname=" + URLEncoder.encode("我的目的地", "utf-8"));
            //目的地经纬度
            stringBuilder.append("&lat=" + mode.getEndLatitude());
            stringBuilder.append("&lon=" + mode.getEndLongitude());
            stringBuilder.append("&dev=1&style=2");
            //调用高德地图APP
            Intent intent = new Intent();
            intent.setPackage("com.autonavi.minimap");
            intent.addCategory(Intent.CATEGORY_DEFAULT);
            intent.setAction(Intent.ACTION_VIEW);
            //传递组装的数据
            intent.setData(Uri.parse(stringBuilder.toString()));
            context.startActivity(intent);
        } catch (Exception e) {
            Toast.makeText(context, "地图导航失败，请检查参数是否正确", Toast.LENGTH_SHORT).show();
            e.printStackTrace();
        }
    }

}
